package com.cai.servlet.common.user;

import com.cai.dao.DetailedTopicDao;
import com.cai.dao.impl.DetailedTopicDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * @author RuoLi
 * 获取随机一组题目
 */
@WebServlet(name = "ServletGetRandomTopicCount5", value = "/common/GetRandomTopicCount")
public class ServletGetRandomTopicCount5 extends HttpServlet {
    static int RandomCount=0;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String count = req.getParameter("RandomStatus");
        RandomCount=Integer.parseInt(count);
        HttpSession session= req.getSession();
        session.setAttribute("GetRandomTopicCount",1);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session= req.getSession();
        if (RandomCount==1) {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            DetailedTopicDao dao = new DetailedTopicDaoImpl();
            String data = dao.getRandomTopicCount5();
            session.setAttribute("GetRandomTopicCount",0);
            resp.getWriter().write(data);
        }else {
            session.setAttribute("GetRandomTopicCount",0);
            resp.getWriter().write("0");
        }
    }
}
